package com.busionline.wotu.baselibrary.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.busionline.wotu.baselibrary.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Activity基类(不要修改)
 * Created by woochen123 on 2017/7/26.
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected Context mContext;
    private FrameLayout mFrameLayout;
    protected Toolbar mToolbar;
    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_base);
        mToolbar = findViewById(R.id.toolbar);
        boolean isToolBar = initToolBar();
        if (isToolBar) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onNavigationClick();
                }
            });
        } else {
            mToolbar.setVisibility(View.GONE);
        }
        mFrameLayout = findViewById(R.id.frameLayout);
        initContent(setContentView());
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
    }

    private void onNavigationClick() {
        finish();
    }

    protected abstract boolean initToolBar();

    private void initContent(int layoutId) {
        if (layoutId != 0) {
            View inflate = LayoutInflater.from(mContext).inflate(layoutId, mFrameLayout, false);
            mFrameLayout.addView(inflate);
        }
    }

    /**
     * 设置布局
     */
    protected abstract int setContentView();

    /**
     * 启动activity
     *
     * @param clazz
     */
    protected void startActivity(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    /**
     * 初始化布局
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();


    @Override
    public void onBackPressed() {
        back();
    }

    /**
     * 返回
     */
    protected void back() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(unbinder != null){
            unbinder.unbind();
        }
    }

}
